package cn.xm.org.controller;


import cn.xm.basic.anno.LogAnno;
import cn.xm.basic.aop.PreAuthorize;
import cn.xm.basic.util.AjaxResult;
import cn.xm.basic.util.LoginContext;
import cn.xm.basic.util.PageList;
import cn.xm.org.domain.Department;
import cn.xm.org.query.DepartmentQuery;
import cn.xm.org.service.DepartmentService;
import cn.xm.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @PutMapping
    @ApiOperation(value = "增加或修改部门" )
    @PreAuthorize(sn = "department:save",name = "保存")
    public AjaxResult addOrUpdate(@ApiParam(value="部门实体类",required = true) @RequestBody Department department) {
        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return AjaxResult.me().success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(sn = "department:delete",name = "删除")
    public AjaxResult delete(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        departmentService.delete(id);
        return AjaxResult.me().success();
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
    @PreAuthorize(sn = "department:batchDelete",name = "批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        departmentService.batchDelete(ids);
        return AjaxResult.me().success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(sn = "department:findById",name = "靠id查询")
    public Department findById(@ApiParam(value="ID属性",required = true) @PathVariable("id") Long id) {
        Department department = departmentService.findById(id);
        return department;

    }

    @GetMapping
    @ApiOperation(value = "查询所有" )
    @PreAuthorize(sn = "department:findAll",name = "查询所有")
    public List<Department> findAll() {
        return departmentService.findAll();
    }

    @PostMapping
    @ApiOperation(value = "分页查询" )
    @LogAnno(method = "queryByPage",methodDes = "分页查询")
    @PreAuthorize(sn = "department:queryByPage",name = "分页查询")
    public PageList<Department> queryByPage(@ApiParam(value="分页条件封装的类",required = true) @RequestBody DepartmentQuery departmentQuery, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return departmentService.queryByPage(departmentQuery);
    }

    @PostMapping("/tree")
    @PreAuthorize(sn = "department:tree",name = "部门的无穷级树")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
